<?php
namespace Admin\Controller;
use Think\Controller;
class RoleController extends CommonController
{	
	public $role ;

	public $auth ;

	public $role_auth ;


	public function _initialize(){
		$this->role = D('role as a');
		$this->auth = M('auth');
		$this->role_auth = D('role_auth');
	}

	public function showlist(){

		$roles = $this->role->select();
		$role_auth = $this->role->field(['a.role_id','b.auth_name','b.auth_id'])->join('rl_role_auth as c on c.role_id = a.role_id')->join('rl_auth as b on b.auth_id = c.auth_id')->select();
		
		// $auths = $this->
		$auths = $this->auth->select();//查找权限列表
		$list = $this->get_list($auths);

		foreach($list as $val){
			foreach($val['child'] as $va){
				$au[] = $va['auth_id'];
			}
		}
		
		$this->assign('role_auth',$role_auth);
		$this->assign('au',$au);
		$this->assign('roles',$roles);
		$this->display();
	}

	public function append(){
		$auths = $this->auth->select();//查找权限列表
		$list = $this->get_list($auths);//权限列表无极分类
		if(IS_POST){

			
			$info = ['code' => 0 , 'info' => '' , 'statu' => 'error'];
			$data = I('post.');
			$data['auths'][] = '1';
			// var_dump($data);die;
			
			//遍历添加权限
			if(!$this->role->create()){
				$info['info'] = $this->role->getError();
				$this->ajaxReturn($info);
			}else{
				$role = $this->role->add();
				$data['role_id'] = $role;
				if($role){
					foreach($data['auths'] as $val){
						$data['auth_id'] = $val;
						$this->role_auth->add($data);
					}
					$info['code'] = 1;
					$info['info'] = '添加成功';
					$info['status'] = 'success';
				}

				$this->ajaxReturn($info);
			}
			
			

			
		}

		$this->assign('list',$list);
		$this->display();
	}

	public function edit(){

		$auths = $this->auth->select();//查找权限列表
		$list = $this->get_list($auths);//权限列表无极分类

		$role_id = I('get.role_id');//获取角色ID

		$permission = $this->role_auth->where(array('role_id'=>$role_id))->select();//获取已有权限
		$role = $this->role->where(array('role_id'=>$role_id))->find();//获取角色数据

		$arr_auth = array();
		foreach($permission as $val){
			$arr_auth[] = $val['auth_id'];
		}//已有权限组合数据
		if(IS_POST){
			$info = ['code' => 0 , 'info' => '' , 'status' => 'error'];
			$data = I('post.');

			$data['auths'][] = '1';

			$arr_add = array_diff($data['auths'], $arr_auth);
			$arr_del = array_diff($arr_auth, $data['auths']);
			// var_dump($data['role_id']);die;
			if(!$this->role->create($data,2)){
				$info['info'] = $this->role->getError();
				$this->ajaxReturn($info);
			}else{
				$result =  $this->role->where(array('role_id'=>$data['role_id']))->save();

				if($arr_add!=''){
					foreach($arr_add as $val){
						$add = $this->role_auth->add(['role_id'=>$data['role_id'],'auth_id'=>$val]);
					}
				}//添加增加的权限

				if($arr_del!=''){
					foreach($arr_del as $val){
						$del = $this->role_auth->where(['role_id'=>$data['role_id'],'auth_id'=>$val])->delete();
					}
				}//删除减少的权限

				if($result || $add || $del){

					$info['code'] = 1;
					$info['info'] = '修改成功';
					$info['status'] = 'success';
					$this->ajaxReturn($info);
				}else{
					$info['info'] ='修改失败';
					$this->ajaxReturn($info); 
				}

			}
		}
		

		$this->assign('role',$role);
		$this->assign('arr_auth',$arr_auth);
		$this->assign('list',$list);
		$this->display();
	}

	public function del(){
		if(IS_POST){
			$info = ['code' => 0 , 'info' => '' , 'status' => 'error'];
			$role_id = I('post.role_id');

			$where = ['role_id'=>$role_id];
			$result = $this->role->where($where)->delete(); 
			if($result){
				$del_auth = $this->role_auth->where($where)->delete();
				if($del_auth){
					$info['code'] = 1;
					$info['info'] = '删除成功';
					$info['status'] = 'success';
					$this->ajaxReturn($info);
 				}else{
					$info['info'] = '删除异常';
					$this->ajaxReturn($info);
				}
			}else{
				$info['info'] = '删除失败';
				$this->ajaxReturn($info);
			}
		}
	}

	public function get_list($array,$pid=0){
		$list = array();

		foreach($array as $vo){
			if($vo['pid'] == $pid){
				$child = $this->get_list($array,$vo['auth_id']);
				$vo['child'] = $child;
				$list[] = $vo;
			}
		}

		return $list;
	}

}
